@extends('admin.layouts.app')

@section('title', '车主管理 - 挪车小助手后台管理')

@section('content')
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">车主管理</h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        <div class="btn-group me-2">
            <button type="button" class="btn btn-sm btn-outline-secondary" onclick="window.print()">
                <i class="bi bi-printer"></i> 打印
            </button>
            <a href="{{ route('admin.car-owners.export') }}" class="btn btn-sm btn-outline-secondary">
                <i class="bi bi-download"></i> 导出
            </a>
        </div>
    </div>
</div>

<div class="row mb-3">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                车主筛选
            </div>
            <div class="card-body">
                <form action="{{ route('admin.car-owners.index') }}" method="GET" class="row g-3">
                    <div class="col-md-3">
                        <label for="search" class="form-label">搜索</label>
                        <input type="text" class="form-control" id="search" name="search" placeholder="用户昵称/手机号码" value="{{ request('search') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="date_from" class="form-label">注册日期从</label>
                        <input type="date" class="form-control" id="date_from" name="date_from" value="{{ request('date_from') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="date_to" class="form-label">注册日期至</label>
                        <input type="date" class="form-control" id="date_to" name="date_to" value="{{ request('date_to') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="scan_count" class="form-label">最少扫码次数</label>
                        <input type="number" class="form-control" id="scan_count" name="scan_count" min="0" value="{{ request('scan_count') }}">
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary">筛选</button>
                        <a href="{{ route('admin.car-owners.index') }}" class="btn btn-secondary">重置</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                车主列表
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>用户昵称</th>
                                <th>联系电话</th>
                                <th>注册时间</th>
                                <th>扫码次数</th>
                                <th>电话拨打次数</th>
                                <th>QR码状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse ($carOwners as $carOwner)
                                <tr>
                                    <td>{{ $carOwner->id }}</td>
                                    <td>
                                        @if($carOwner->user && $carOwner->user->avatar_url)
                                            <img src="{{ $carOwner->user->avatar_url }}" alt="头像" class="rounded-circle" width="30" height="30">
                                        @endif
                                        {{ $carOwner->user ? $carOwner->user->nickname : '未知用户' }}
                                    </td>
                                    <td>{{ $carOwner->phone_number }}</td>
                                    <td>{{ $carOwner->created_at }}</td>
                                    <td>{{ $carOwner->scans_count ?? 0 }}</td>
                                    <td>{{ $carOwner->calls_count ?? 0 }}</td>
                                    <td>
                                        @if($carOwner->qr_code_path)
                                            <span class="badge bg-success">已生成</span>
                                        @else
                                            <span class="badge bg-warning">未生成</span>
                                        @endif
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{{ route('admin.car-owners.show', $carOwner->id) }}" class="btn btn-info">
                                                <i class="bi bi-eye"></i>
                                            </a>
                                            @if($carOwner->qr_code_path)
                                                <a href="{{ url($carOwner->qr_code_path) }}" class="btn btn-primary" target="_blank">
                                                    <i class="bi bi-qr-code"></i>
                                                </a>
                                                <button type="button" class="btn btn-warning" 
                                                    data-bs-toggle="modal" 
                                                    data-bs-target="#regenerateQrModal" 
                                                    data-id="{{ $carOwner->id }}">
                                                    <i class="bi bi-arrow-repeat"></i>
                                                </button>
                                            @else
                                                <button type="button" class="btn btn-success" 
                                                    data-bs-toggle="modal" 
                                                    data-bs-target="#generateQrModal" 
                                                    data-id="{{ $carOwner->id }}">
                                                    <i class="bi bi-plus-circle"></i>
                                                </button>
                                            @endif
                                        </div>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="8" class="text-center">暂无车主数据</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="d-flex justify-content-center mt-3">
                    {{ $carOwners->appends(request()->except('page'))->links() }}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 生成二维码确认弹窗 -->
<div class="modal fade" id="generateQrModal" tabindex="-1" aria-labelledby="generateQrModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="generateQrModalLabel">生成二维码</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                确定要为这位车主生成二维码吗？
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="generateQrForm" action="" method="POST">
                    @csrf
                    <button type="submit" class="btn btn-success">确认生成</button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- 重新生成二维码确认弹窗 -->
<div class="modal fade" id="regenerateQrModal" tabindex="-1" aria-labelledby="regenerateQrModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="regenerateQrModalLabel">重新生成二维码</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="alert alert-warning">
                    重新生成后，原有二维码将失效，确定要继续吗？
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="regenerateQrForm" action="" method="POST">
                    @csrf
                    @method('PUT')
                    <button type="submit" class="btn btn-warning">确认重新生成</button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 生成二维码弹窗处理
        const generateQrModal = document.getElementById('generateQrModal');
        generateQrModal.addEventListener('show.bs.modal', function (event) {
            const button = event.relatedTarget;
            const carOwnerId = button.getAttribute('data-id');
            
            const generateQrForm = document.getElementById('generateQrForm');
            generateQrForm.action = `/admin/car-owners/${carOwnerId}/generate-qr`;
        });
        
        // 重新生成二维码弹窗处理
        const regenerateQrModal = document.getElementById('regenerateQrModal');
        regenerateQrModal.addEventListener('show.bs.modal', function (event) {
            const button = event.relatedTarget;
            const carOwnerId = button.getAttribute('data-id');
            
            const regenerateQrForm = document.getElementById('regenerateQrForm');
            regenerateQrForm.action = `/admin/car-owners/${carOwnerId}/regenerate-qr`;
        });
    });
</script>
@endsection 